﻿using AFanDa.BLL.BLLService;
using AFanDa.Dto.Base;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AFanDa.Admin.Controllers
{
    /// <summary>
    /// 商品信息 控制器
    /// </summary>
    public class GoodsController : Controller
    {
        GoodsService _goodsService = null;

        /// <summary>
        /// 构造函数 初始化
        /// </summary>
        public GoodsController()
        {
            _goodsService = new GoodsService();
        }


        // GET: Goods
        /// <summary>
        /// 商品控制台页面
        /// </summary>
        /// <returns></returns>
        public ActionResult GoodsInfo()
        {
            return View();
        }
        /// <summary>
        /// 添加商品类型页面
        /// </summary>
        /// <returns></returns>
        public ActionResult GoodsTypeHtml()
        {
            return View();
        }

        /// <summary>
        /// 获取 商品类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetGoodsType(string keys)
        {
            keys = keys ?? "";
            var reuslt = _goodsService.GetGoodsType(keys);
            return Json(reuslt, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 添加商品标签页面
        /// </summary>
        /// <returns></returns>
        public ActionResult AddGoodsLabelHtml()
        {
            return View();
        }
        /// <summary>
        /// 添加商品类型
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddGoodsType()
        {

            HttpRequestBase HttpFile = Request;

            string name = Request.Form["name"];
            string spare = Request.Form["spare"];

            HttpPostedFileBase file = Request.Files[0];


            string Gtid = Common.Config.GUID();

            string FileName = file.FileName;

            string hzm = FileName.Substring(FileName.LastIndexOf('.'));

            string NewName = Common.Config.GUID() + hzm;

            string Path = $@"/Source/GoodsTypeImg/{Gtid}/{NewName}";

            var result = _goodsService.AddGoodType(new Dto.Request.GoodsTypeRequestDto()
            {
                id = Gtid,
                name = name,
                spare = spare,
                img = Path
            });
            if (result.code == 0)
            {
                if (!Directory.Exists(Server.MapPath($"/Source/GoodsTypeImg/{Gtid}")))
                {
                    Directory.CreateDirectory(Server.MapPath($"/Source/GoodsTypeImg/{Gtid}"));
                };
                file.SaveAs(Server.MapPath(Path));
            }




            return Json(result);
        }


        /// <summary>
        /// 添加商品标签
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddGoodsLabel(Dto.Request.GoodsLabelRequestDto dto)
        {
            var reuslt = _goodsService.AddGoodsLabel(dto);
            return Json(reuslt);
        }


        /// <summary>
        /// 根据商品类型 id 查找 商品标签
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetGoodslabelByGTid(string id, string keys)
        {
            keys = keys ?? "";
            var result = _goodsService.GetGoodslabelByGTid(new Dto.Request.GoodsTypeRequestDto()
            {
                id = id,
                key = keys

            });

            return Json(result, JsonRequestBehavior.AllowGet);

        }

        /// <summary>
        /// 添加商品 页面 （彭思期 负责）
        /// </summary>
        /// <returns></returns>
        public ActionResult AddGoodsHtml()
        {
            return View();
        }

        /// <summary>
        /// 口味类型详情 页面
        /// </summary>
        /// <returns></returns>
        public ActionResult FlavorTypeHtml()
        {
            return View();
        }
        /// <summary>
        /// 口味页面
        /// </summary>
        /// <returns></returns>
        public ActionResult FlavorHtml()
        {
            return View();

        }

        /// <summary>
        /// 添加口味类型
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddFlavorType(Dto.Request.FlavorTypeRequestDto dto)
        {
            var resutl = _goodsService.AddFlavorType(dto);
            return Json(resutl);
        }
        /// <summary>
        /// 获取口味类型
        /// </summary>
        /// <param name="keys"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetFlavorType(string keys = "")
        {
            keys = keys ?? "";

            var result = _goodsService.GetFlavorType(new Dto.Request.FlavorTypeRequestDto()
            {
                name = keys,
            });

            return Json(result, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateFlavorType(Dto.Request.FlavorTypeRequestDto dto)
        {
            var result = _goodsService.UpdateFlavorType(dto);

            return Json(result);
        }



        /// <summary>
        /// 添加商品
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddGoods()
        {

            HttpRequestBase HttpFile = Request;

            string str = Request.Form["goods"];
            Dto.Request.GoodsRequestDto dto = Newtonsoft.Json.JsonConvert.DeserializeObject<Dto.Request.GoodsRequestDto>(str);

            HttpPostedFileBase file = Request.Files[0];


            string hzm = file.FileName.Substring(file.FileName.IndexOf('.'));

            string newName = Common.Config.GUID() + hzm;
            dto.id = Common.Config.GUID();





            string Path = $@"/Source/GoodsImg/{dto.id}/{newName}";


            dto.img = Path;



            var result = _goodsService.AddGoods(dto);




            if (result.code == ApiCode.ok)
            {
                if (!Directory.Exists(Server.MapPath($"/Source/GoodsImg/{ dto.id}")))
                {
                    Directory.CreateDirectory(Server.MapPath($"/Source/GoodsImg/{ dto.id}"));
                }
                file.SaveAs(Server.MapPath(Path));
            }



            return Json(result);








        }


        /// <summary>
        /// 商品类型页面
        /// </summary>
        /// <returns></returns>
        public ActionResult GoodsTypeManager()
        {
            return View();
        }
        /// <summary>
        /// 商品标签页面
        /// </summary>
        /// <returns></returns>
        public ActionResult GoodsLabelManager()
        {
            return View();

        }


        /// <summary>
        /// 获取口味 根据口味类型
        /// </summary>
        /// <param name="keys"></param>
        /// <param name="ftid"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetFlavor(string keys, string ftid)
        {
            keys = keys ?? "";

            var result = _goodsService.GetFlavor(new Dto.Request.flavorRequestDto()
            {
                Ftid = ftid,
                key = keys
            });

            return Json(result, JsonRequestBehavior.AllowGet);


        }
        /// <summary>
        /// 添加口味
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddFlavor(Dto.Request.flavorRequestDto dto)
        {
            var result = _goodsService.AddFlavor(dto);

            return Json(result);
        }

        /// <summary>
        /// 修改口味
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateFlavor(Dto.Request.flavorRequestDto dto)
        {
            var result = _goodsService.UpdateFlavor(dto);
            return Json(result);
        }


        /// <summary>
        /// 修改商品标签
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateGoodLabel(Dto.Request.GoodsLabelRequestDto dto)
        {

            var result = _goodsService.UpdateGoodLabel(dto);
            return Json(result);

        }

        /// <summary>
        /// 修改商品类型页面
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdateGoodTypeHtml()
        {
            return View();
        }
        [HttpPost]
        public ActionResult UpdateGoodType()
        {

            HttpRequestBase HttpFile = Request;

            string str = Request.Form["goods"];
            Dto.Request.GoodsTypeRequestDto dto = Newtonsoft.Json.JsonConvert.DeserializeObject<Dto.Request.GoodsTypeRequestDto>(str);
            HttpPostedFileBase img = Request.Files[0];

            string hzm = img.FileName.Substring(img.FileName.IndexOf('.'));

            string NewName = Common.Config.GUID() + hzm;


            string Path = $@"/Source/GoodsTypeImg/{dto.id}/{NewName}";




            dto.img = Path;

            var result = _goodsService.UpdateGoodsType(dto);

            if (result.code == ApiCode.ok)
            {


                img.SaveAs(Server.MapPath(Path));
            }



            return Json(result);



        }
        /// <summary>
        /// 获取所有商品
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetAllGoods()
        {
            var result = _goodsService.GetAllGoods();

            return Json(result, JsonRequestBehavior.AllowGet);

        }


        /// <summary>
        /// 获取商品
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult getGoods(string key, string Glid, int status)
        {
            key = key ?? "";

            var result = _goodsService.getGoods(new Dto.Request.GoodsRequestDto()
            {
                key = key,
                Glid = Glid,
                status = status

            });

            return Json(result, JsonRequestBehavior.AllowGet);

        }

        /// <summary>
        /// 修改商品状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateGoodStatus(string id, int status)
        {
            var result = _goodsService.UpdateGoodStatus(new Dto.Request.GoodsRequestDto()
            {
                id = id,
                status = status
            });
            return Json(result);
        }



        /// <summary>
        /// 商品详情页面
        /// </summary>
        /// <returns></returns>
        public ActionResult GoodHtml()
        {
            return View();
        }

        /// <summary>
        /// 获取单个商品
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetGoodsById(string id)
        {
            var result = _goodsService.GetGoodsById(new Dto.Request.GoodsRequestDto()
            {
                id = id
            });
            return Json(result,JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 商品详情页面
        /// </summary>
        /// <returns></returns>
        public ActionResult GoodDetailsHtml()
        {

            return View();
        }

    }
}
